/<template>
  <div class="login">
      <div class="loginWrap">
        <h1>登录</h1>
        <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="100px" class="demo-ruleForm">
          <el-form-item label="账号" prop="pass">
            <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="checkPass">
            <el-input type="password" v-model="ruleForm.pwd" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="checkPass">
            <el-input type="text" v-model="ruleForm.text" autocomplete="off"></el-input>
            <span v-html="code"></span>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button @click="registry">注册</el-button>
          </el-form-item>
        </el-form>
      </div>
  </div>
</template>

<script>
import {loginApi,registryApi,getCode} from "../../api/api"

export default {
  data () {
    return {
      ruleForm:{
        username:"",
        pwd:"",
        text:""
      },
      code:""
    }
  },
  async mounted () {
      // 在登陆页的时候清空本地存储token
      localStorage.clear()

      const {data}=await getCode()
      this.ruleForm.text=data.text
      this.code=data.svg
  },
  methods: {
    async login (){
      const {data}=await loginApi(this.ruleForm.username,this.ruleForm.pwd,this.ruleForm.text)

      alert(data.msg)
      localStorage.setItem("token",data.token)
      //登录成功跳转页面
      this.$router.push('/home')
    },
    // 点击注册
    async registry(){
      const {data}=await registryApi(this.ruleForm.username,this.ruleForm.pwd)
      console.log(data);
    }
  }
}
</script>

<style scoped>
.login{
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

.loginWrap{
    width: 500px;
    height: 400px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -250px;
    margin-top: -200px;
    border: 1px solid #ccc;
    border-radius: 5px;
}

.el-input{
  width: 300px;
}
</style>